layui.use(['form','layer','laydate','table','laytpl'],function(){
    var form = layui.form,
        layer = parent.layer === undefined ? layui.layer : top.layer,
        $ = layui.jquery,
        laydate = layui.laydate,
        laytpl = layui.laytpl,
        table = layui.table;

    //列表
    var tableIns = table.render({
        elem: '#entityList',
        url : '/record/JsonList?introductUserId='+$("#introductUserId").val(),
        cellMinWidth : 95,
        page : true,
        height : "full-125",
        limit : 20,
        method:'post',
        limits : [10,15,20,25],
        id : "entityList",
        cols : [[
            {field: 'id', title: '订单号',align:'center',width:210},
            {field: 'memberId', title: '会员ID',align:'center',width:80},
            {field: 'nickName', title: '昵称',align:'center',width:130},
            {field: 'name', title: '姓名',align:'center',width:100},
            {field: 'phone', title: '手机号',align:'center',width:120},
            {field: 'type', title: '类型',align:'center',width:100,templet:function(d){
                if(d.type=="1"){
                    return "消费记录";
                }else if(d.type=="2"){
                    return "充值记录";
                }
            }},
            {field: 'descripts', title: '描述',align:'center'},
            {field: 'createtime', title: '创建时间', align:'center', width:170, templet:function(d){
                return d.createtime;
            }}
        ]]
    });

    //搜索
    $(".search_btn").on("click",function(){
        table.reload("entityList",{
            page: {
                curr: 1 //重新从第 1 页开始
            },
            where: {
                memberId: $("#memberId").val(),
                nickName: $("#nickName").val(),
                phone: $("#phone").val(),
                type: $("#type").val()
            }
        })
    });

    //增加/编辑
    function addEntity(params){
        var title="编辑";
        if(params==null||params==""){
            title="添加"
        }
        var index = layui.layer.open({
            title :title,
            type : 2,
            content : "/record/SelectEntity"+params,
            success : function(layero, index){
                var body = layui.layer.getChildFrame('body', index);
                body.find(".closeLay").bind("click",function () {
                    layui.layer.close(index);
                })
                layui.layer.full(index);
            }
        });
    }
    $(".add_btn").click(function(){
        addEntity('');
    })

    //列表操作
    table.on('tool(entityList)', function(obj){
        var layEvent = obj.event,
            data = obj.data;
        if(layEvent === 'edit'){ //编辑
            addEntity("?id="+data.id);
        } else if(layEvent === 'del'){ //删除
            layer.confirm('是否删除设备类型【'+data.name+'】吗？',{icon:3, title:'提示信息'},function(index){
                $.get("/record/DeleteEntity",{
                    "id":data.id,
                    "_method":"DELETE"
                },function(result){
                    if(1==result.returnInfo.state){
                        tableIns.reload();
                        layer.close(index);
                    }else{
                        top.layer.msg(result.returnInfo.errorMsg);
                    }
                })
            });
        }
    });

})